package com.tpp.base.controller;

import com.tpp.base.elastic.ItemDto;
import com.tpp.base.service.ItemService;
import com.tpp.base.service.ItemTypeService;
import com.tpp.base.vo.*;
import com.tpp.comm.util.JwtUtil;
import com.tpp.comm.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class ItemController {

    @Resource
    private ItemTypeService itemTypeService;

    @Resource
    private ItemService itemService;

    @Resource
    private JwtUtil jwtUtil;

    @PostMapping("/p/queryItemLike")
    public Result queryItemPager(@RequestBody ItemDto itemDto){
        return Result.success(itemService.queryItemPager(itemDto));
    }

    @GetMapping("/p/queryLineNav")
    public Result queryLineNav(){
        List<LineNavVo> lineNavVos = itemTypeService.queryLineNav();
        return Result.success(lineNavVos);
    }

    @GetMapping("/p/queryNav")
    public Result queryNav(){
        List<NavLevel01> navLevel01s = itemTypeService.queryNav();
        return Result.success(navLevel01s);
    }

    @PostMapping("/p/queryFloorItems")
    public Result queryFloorItems(){
        return Result.success(itemService.queryFloorItems());
    }

    @GetMapping("/p/queryItemDetail")
    public Result queryItemDetail(Integer id){
        ItemDetailVo itemDetailVo = itemService.queryItemDetail(id);
        return Result.success(itemDetailVo);
    }

    @GetMapping("/p/queryItemTypeByParent")
    public Result queryItemTypeByParent(Integer parent){
        return Result.success(itemTypeService.queryItemTypeByParent(parent));
    }

    @GetMapping("/p/queryTodayRecommend")
    public Result queryTodayRecommend(){
       List<ItemRecommendVo> todayRecommends = itemService.queryTodayRecommend();
       return Result.success(todayRecommends);
    }

    @GetMapping("/p/queryToSaleItem")
    public Result queryToSaleItem(){
        List<ItemRecommendVo> todayRecommends = itemService.queryToSaleItem();
        return Result.success(todayRecommends);
    }

    @GetMapping("/p/queryHotItems")
    public Result queryHotItems(@RequestParam(required = false)Integer itemTypeId){
        List<ItemRecommendVo> todayRecommends = itemService.queryHotItems(itemTypeId);
        return Result.success(todayRecommends);
    }

    @GetMapping("/p/queryItemRecommend")
    public Result queryItemRecommend(@RequestParam(required = false)Integer itemTypeId,@RequestParam(required = false) Integer limit){
        List<ItemRecommendVo> todayRecommends = itemService.queryItemRecommend(itemTypeId,limit);
        return Result.success(todayRecommends);
    }

    @PostMapping("/p/queryItemComment")
    public Result queryItemComment(@RequestBody ItemCommentPage itemCommentPage){
        ItemCommentPage itemCommentVos = itemService.queryItemComment(itemCommentPage);
        return Result.success(itemCommentVos);
    }

    @PostMapping("/v/commitItemComment")
    public Result commitItemComment(@RequestBody AddCommentVo addCommentVo,@RequestHeader("token") String token){
        try {
            addCommentVo.setUserId(jwtUtil.parseJwt(token));
            itemService.commitItemComment(addCommentVo);
            return Result.success("添加剧评成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail("添加评论失败");
        }
    }


}
